package com.home.webapp.monitor.service.impl;

import com.home.base.core.framework.msg.PageResponse;
import com.home.common.framework.core.service.impl.BaseServiceImpl;
import com.home.common.handler.exception.BizException;
import com.home.common.redis.RedisUtil;
import com.home.common.utils.query.QueryParams;
import com.home.security.core.enums.AppClientEnum;
import com.home.webapp.monitor.entity.BaseUserOnline;
import com.home.webapp.monitor.mapper.BaseUserOnlineMapper;
import com.home.webapp.monitor.service.BaseUserOnlineService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.session.UnknownSessionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Description: 在线用户记录 serviceImpl
 * Created by 王大宸 on 2020-10-12 22:27:31
 * Created with IntelliJ IDEA.
 */
@Service
public class BaseUserOnlineServiceImpl extends BaseServiceImpl<BaseUserOnlineMapper, BaseUserOnline, String> implements BaseUserOnlineService {
    private static final Logger logger = LoggerFactory.getLogger(BaseUserOnlineServiceImpl.class);

    @Override
    public PageResponse<BaseUserOnline> page(Map<String, Object> params) {
        QueryParams queryParams = new QueryParams(params);
        Page<BaseUserOnline> page = PageHelper.startPage(queryParams.getPage(), queryParams.getLimit());
        List<BaseUserOnline> list = baseMapper.page(params);
        for (BaseUserOnline online : list) {
            online.setClientId(AppClientEnum.getClientName(online.getClientId()));
        }
        return PageResponse.ok(page.getTotal(), list);
    }

    /***
     * 功能说明：强退
     *
     * @author 王大宸
     * @date 2020/10/14 22:08
     * @param id id
     * @return com.home.common.framework.msg.ServerResponse<com.home.webapp.monitor.entity.BaseUserOnline>
     */
    @Override
    public int remove(String id) throws BizException {
        BaseUserOnline userOnline = baseMapper.selectByPrimaryKey(id);
        try {
            if (null == userOnline) {
                return super.remove(id);
            }
            //  RedisUtil.remove("","");
            // ShiroUtils.removeCache(userOnline.getLoginName(), userOnline.getClientId(), true);
            return super.remove(id);
        } catch (UnknownSessionException e) {
            logger.error("强退用户失败: ", e);
            throw new BizException("未找到该用户登录信息");
        }
    }
}
